package cn.zd.demo1.contentprovider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

import cn.zd.demo1.sql.UserDBHelper;
import cn.zd.demo1.util.ContentProviderUserInfoConstant;

public class TabUserInfoProvider extends ContentProvider {
    private UserDBHelper helper;
    private static final String DB_TABLE_NAME="tbl_cp_userinfo";
    private static final String SQL_CREATE = "CREATE TABLE  IF NOT EXISTS "+DB_TABLE_NAME+" (" +
            "  user_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
            "  user_name VARCHAR NOT NULL," +
            "  user_age INTEGER NOT NULL" +
            ");";

    private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
    private static final int USER = 1;

    private static final int USERS = 2;

    static {
        URI_MATCHER.addURI(ContentProviderUserInfoConstant.AUTHORITIES,"/user",USERS);
        URI_MATCHER.addURI(ContentProviderUserInfoConstant.AUTHORITIES,"/user/#",USER);
    }

    @Override
    public String getType(Uri uri) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public boolean onCreate() {
        helper = UserDBHelper.getInstance(getContext(),1);
        helper.getWritableDatabase().execSQL(SQL_CREATE);
        return true;
    }
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        long rowid = helper.openWrite().insert(DB_TABLE_NAME,null,values);
        helper.openWrite().close();
        if(rowid > 0) {
            // 生成新的uri
            Uri newUri = ContentUris.withAppendedId(ContentProviderUserInfoConstant.CONTENT_URI,rowid);
            /**
             *通知观察者，具体详见 {@link  cn.zd.demo1.MyContentObserverActivity}
              */
            getContext().getContentResolver().notifyChange(newUri,null);
        }
        return uri;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int count = 0;
        switch (URI_MATCHER.match(uri)) {
            case USER:
                String id = uri.getLastPathSegment();
                count = helper.openWrite().delete(DB_TABLE_NAME,"user_id=?",new String[]{id});
                break;
            case USERS:
                count = helper.openWrite().delete(DB_TABLE_NAME,selection,selectionArgs);
                break;
            default:
                Log.i("ContentPrivder","无效的URI");
        }
        helper.openWrite().close();
        return count;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        if(URI_MATCHER.match(uri) == USERS) {
            return helper.openRead().query(DB_TABLE_NAME, projection, selection, selectionArgs, null, null, null);
        }
        Log.i("ContentProvider","不合法的访问uri contentProvider");
        return null;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        int count = helper.openWrite().update(DB_TABLE_NAME,values,selection,selectionArgs);
        helper.openWrite().close();
        return count;
    }
}